<template>
  <v-layout type="main">
    <!-- <p class="page-title">我的发布</p> -->
    <!-- <el-radio-group v-model="showThisActive" size="medium" @change="getData">
      <el-radio-button :label="0">数据集</el-radio-button>
      <el-radio-button :label="1">算法</el-radio-button>
      <el-radio-button :label="2">模型</el-radio-button>
    </el-radio-group> -->

    <el-tabs v-model="nowRouter" @tab-click="getData">
      <el-tab-pane label="数据集" name="0"></el-tab-pane>
      <el-tab-pane label="算法" name="1"></el-tab-pane>
      <el-tab-pane label="模型" name="2"></el-tab-pane>
    </el-tabs>

    <el-table :data="dataList" v-show="showThisActive == 0">
      <el-table-column prop="name" label="名称">
        <template slot-scope="{ row }">
          <el-popover placement="right-start" width="320" trigger="hover">
            <row-data :row="row" />
            <el-button type="text" @click="$router.push({ path: '/my_market/code', query: { id: row.id } })"
              slot="reference">{{ row.name }}</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审批状态">
        <template slot-scope="{ row }">
          <p v-if="row.status == 0">等待审核</p>
          <p v-else-if="row.status == 1">审核通过</p>
          <p v-else-if="row.status == 2">未通过</p>
          <p v-else>未知状态，请联系管理员</p>
        </template>
      </el-table-column>
      <el-table-column prop="create_user.nick_name" label="发布人" />
      <el-table-column prop="update_time" label="发布时间" />
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @click="del(row)" v-if="row.status != 1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table :data="dataList1" v-show="showThisActive == 1">

      <el-table-column label="名称">
        <template slot-scope="{ row }">
          <!-- <el-popover placement="right-start" width="320" trigger="hover"> -->
          <row-data :row="row" />
          <el-button type="text"
            @click="$router.push({ path: '/my_market/code', query: { id: row.algorithm.id, version_id: row.id, active: 1 } })"
            slot="reference">{{ row.algorithm.name }}</el-button>
          <!-- </el-popover> -->

        </template>
      </el-table-column>
      <el-table-column prop="version" label="版本" />
      <el-table-column prop="status" label="审批状态">
        <template slot-scope="{ row }">
          <p v-if="row.status == 0">等待审核</p>
          <p v-else-if="row.status == 1">审核通过</p>
          <p v-else-if="row.status == 2">未通过</p>
          <p v-else>未知状态，请联系管理员</p>
        </template>
      </el-table-column>
      <el-table-column prop="create_user.nick_name" label="发布人" />
      <el-table-column prop="update_time" label="发布时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @click="del1(row)" v-if="row.status != 1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table :data="dataList2" v-show="showThisActive == 2">

      <el-table-column prop="name" label="名称">
        <template slot-scope="{ row }">
          <el-popover placement="right-start" width="320" trigger="hover">
            <row-data :row="row" />
            <el-button type="text" @click="$router.push({ path: '/my_market/code', query: { id: row.id } })"
              slot="reference">{{ row.model.name }}</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="version" label="版本" />
      <el-table-column prop="status" label="审批状态">
        <template slot-scope="{ row }">
          <p v-if="row.status == 0">等待审核</p>
          <p v-else-if="row.status == 1">审核通过</p>
          <p v-else-if="row.status == 2">未通过</p>
          <p v-else>未知状态，请联系管理员</p>
        </template>
      </el-table-column>
      <el-table-column prop="create_user.nick_name" label="发布人" />
      <el-table-column prop="update_time" label="发布时间" />
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @click="del2(row)" v-if="row.status != 1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box" v-if="Pagedata.total > Pagedata.pageSize">
      <el-pagination background class="pagination" layout="total, prev, pager, next, jumper"
        :page-size="Pagedata.pageSize" :current-page.sync="Pagedata.pageNum" @current-change="handleCurrentChange"
        :total="Pagedata.total">
      </el-pagination>
    </div>
  </v-layout>
</template>

<script>
import { getMyMarketDataSets, delMarketDataSet } from '@/api/marketDataset'
import { getMyMarketModelVersions, delMarketModelVersion } from '@/api/marketModelVersion'
import { getMyMarketAlgorithmVersions, delMarketAlgorithmVersion } from '@/api/marketAlgorithmVersion'
export default {
  data() {
    return {
      nowRouter: 0,
      Pagedata: {
        pageNum: 1,
        pageSize: 12,
        total: 0,
        resourceType: 0,// 数据集
        name: "", // 名称搜索
        order: "recommend", // 排序recommend、createTime、likeCount
      },
      showThisActive: 0,
      dataList: [],
      dataList1: [],
      dataList2: [],
      testPic: require('@/assets/img/test-lis-pic.png'),
      type: process.env.VUE_APP_VERSIONS == 'alpha' ? 1 : 2,
    };
  },
  mounted() {

  },
  async created() {
    this.getData();
  },
  destroyed() {

  },
  methods: {
    async getMarketDataSets() {
      let res = await getMyMarketDataSets({ ...this.Pagedata, visibility: '2' })
      if (res && res.data) {
        let ret = res.data
        //console.log('ret', ret)
        this.dataList = ret.list
        this.Pagedata.total = ret.total
      }
    },
    async getMarketAlgorithms() {
      let res = await getMyMarketAlgorithmVersions({ ...this.Pagedata, type: this.type })
      if (res && res.data) {
        let ret = res.data
        console.log('ret1', ret)
        this.dataList1 = ret.list
        this.Pagedata.total = ret.total
        this.$forceUpdate()
      }
    },
    async getMarketModels() {
      //let res = await getMarketModels({ ...this.Pagedata })
      let res = await getMyMarketModelVersions({ ...this.Pagedata })
      if (res && res.data) {
        let ret = res.data
        console.log('ret2', ret)
        this.dataList2 = ret.list
        this.Pagedata.total = ret.total
      }
    },
    async getData() {
      if (this.showThisActive == 0) {
        this.getMarketDataSets()
      }
      if (this.showThisActive == 1) {
        this.getMarketAlgorithms()
      }
      if (this.showThisActive == 2) {
        this.getMarketModels()
      }
    },

    getDataSet() {
      this.showThisActive = 0
      this.getData()
    },
    getAlgorithm() {
      this.showThisActive = 1
      this.$forceUpdate()
      this.getData()
    },
    getModel() {
      this.showThisActive = 2
      this.getData()
    },

    handleCurrentChange(val) {
      this.Pagedata.current = val
      this.Pagedata.current = val
      this.getData()
    },

    async del(row) {
      await delMarketDataSet({ "id": row.id })
      this.getDataSet();
    },

    async del1(row) {
      await delMarketAlgorithmVersion({ "id": row.id })
      this.getAlgorithm();
    },

    async del2(row) {
      await delMarketModelVersion({ "id": row.id })
      this.getModel();
    }
  }
}
</script>
<style lang="less" scoped>
.add {
  position: absolute;
  right: 28px;
  top: 56px;
}
</style>